package com.bmh.two;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.net.URISyntaxException;

public class MainActivity extends Activity {
    WebView id_web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wap_activity);
        id_web = (WebView) findViewById(R.id.id_web);
        String murl = getIntent().getStringExtra("url");

        Log.i("", "------------->murl"+murl);
        initWebView(murl);
    }

    /**
     * 初始化webView
     */
    @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
    void initWebView(String url) {
        id_web.getSettings().setJavaScriptEnabled(true);
        id_web.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
        //if ROM supports Multi-Touch
        id_web.getSettings().setBuiltInZoomControls(true); //Enab
        id_web.getSettings().setAppCacheEnabled(true);
        id_web.getSettings().setAllowFileAccess(true);
        id_web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);


        //id_web.getSettings().setUserAgentString("StyleWeShopping/" + com.core.util.AndroidUtil.getVersionName(getActivity()) + " Android/" + android.os.Build.VERSION.RELEASE + " (" + android.os.Build.MODEL + ")");
        id_web.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //return super.shouldOverrideUrlLoading(view, url);

                if (url.contains("http:") || url.contains("https:")) {
                    return super.shouldOverrideUrlLoading(view, url);
                } else {

                    Uri uri= Uri.parse(url);
                    try {
                        //                    Uri uri = Uri.parse(url);
                        Intent intent;
                        intent = Intent.parseUri(url,
                                Intent.URI_INTENT_SCHEME);
                        intent.addCategory("android.intent.category.BROWSABLE");
                        intent.setComponent(null);
                        startActivity(intent);
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                    return true;
                }
            }
        });


        id_web.setWebChromeClient(new WebChromeClient());


        id_web.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && id_web.canGoBack()) {  //表示按返回键
                        id_web.goBack();   //后退
                        //webview.goForward();//前进
                        return true;    //已处理
                    }
                }
                return false;
            }
        });


        id_web.loadUrl(url);
    }
}